package com.guomao.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;


//@Component
//@Aspect
public class Log {
    /*public static void logStart(Object... args) {
        System.out.println("计算器的方法开始执行了,参数是" + Arrays.toString(args));
    }*/

/*
    public static void logStart(Method m, Object... args) {
        System.out.println(m.getName() + "方法开始执行了,参数是" + Arrays.toString(args));
    }

    public static void logEnd(Method m, Object result) {
        System.out.println(m.getName() + "方法执行结束了,结果是" + result);
    }

    public static void logException(Method m, Exception e) {
        System.out.println(m.getName() + "方法出现异常了,异常是:" + e);
    }

    public static void logFinal(Method m) {
        System.out.println(m.getName() + "方法最终结束了");
    }*/

//    controller
//    service 一般都需要管理事务

//    public com.guomao.service.serviceImpl.insert/add/update/delete();
//      orderservie
    //ordermapper
    //orderiteam
//    mapper


    //    @Before("execution(public Integer com.guomao.interfaces.CalculatorImpl.add(Integer,Integer))")
//    @Before("execution(public * com.guomao.interfaces.CalculatorImpl.*(..))")
//    @Before("execution( * com.guomao.interfaces.CalculatorImpl.*(..))")
//    @Before("execution( * *.*.*.CalculatorImpl.*(..))")
//    @Before("execution(* *.*.*(..))")


    @Pointcut("execution(public Integer com.guomao.interfaces.CalculatorImpl.*(Integer,Integer))")
    public void snippet(){

    }

//    @Before("execution(public Integer com.guomao.interfaces.CalculatorImpl.*(Integer,Integer))")

    @Before("snippet()")
    public static void logStart(JoinPoint jp) {
        String name = jp.getSignature().getName();
        Object[] args = jp.getArgs();
        System.out.println(name + "方法开始执行了,参数是" + Arrays.toString(args));
    }

//    @AfterReturning(value = "execution(public Integer com.guomao.interfaces.CalculatorImpl.*(Integer,Integer))", returning = "result")
    @AfterReturning(value = "snippet()", returning = "result")
    public static void logEnd(JoinPoint jp, Object result) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法执行结束了,结果是:" + result);
    }

//    @AfterThrowing(value = "execution(public Integer com.guomao.interfaces.CalculatorImpl.*(Integer,Integer))", throwing = "e")
    @AfterThrowing(value = "snippet()", throwing = "e")
    public static void logException(JoinPoint jp, Exception e) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法出现异常了,异常是:" + e);
    }

//    @After("execution(public Integer com.guomao.interfaces.CalculatorImpl.*(Integer,Integer))")
    @After("snippet()")
    public static void logFinal(JoinPoint jp) {
        String name = jp.getSignature().getName();
        System.out.println(name + "方法最终结束了");
    }

    //    @Around("execution(public Integer com.guomao.interfaces.CalculatorImpl.add(Integer,Integer))")
    public static void logAround(ProceedingJoinPoint pjp) throws Throwable {

        //pjp.proceed() 本质上就是在执行method.invoke()
        try {
            System.out.println("开始...");
            Object result = pjp.proceed();
            System.out.println("返回...");
        } catch (Throwable e) {
            System.out.println("异常...");
        } finally {
            System.out.println("最终...");
        }
    }
}